home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / DIZZY / SRC / MACDIZZY.C < prev    next >
C/C++ Source or Header  |  1991-10-29  |  7KB  |  276 lines

  1. /*
  2. >>    Dizzy 1.0
  3. >>
  4. >>    A digital circuit simulator & design program for the X Window System
  5. >>
  6. >>    Copyright 1990 Juri Munkki, all rights reserved
  7. >>
  8. >>    Please read the included file called "DizzyDoc" for information on
  9. >>    what your rights are concerning this product.
  10. >>
  11. >>    This file contains the main program and even handling for the Macintosh.
  12. >>    Consult DizzyX.c, if you want the Xt/Motif versions of similar routines.
  13. >>
  14. */
  15.  
  16. #define MAIN_PROGRAM
  17. #include    "dizzy.h"
  18. #ifdef MACINTOSH
  19.  
  20. void    CalculatePanelPlaces()
  21. {
  22.     /*    Find out menu pane size from picture size.        */
  23.     MenuR=(*MenuP)->picFrame;
  24.     OffsetRect(&MenuR,4-MenuR.left,4-MenuR.top);
  25.     
  26.     /*    Find tool pane size and move pane below menus.    */
  27.     ToolR=(*ToolP)->picFrame;
  28.     OffsetRect(&ToolR,4-ToolR.left,MenuR.bottom+4-ToolR.top);
  29.     
  30.     /*    Set up the input pin count selector display.    */
  31.     InputSelector=2;
  32.     InputFrame.left=ToolR.left+34;
  33.     InputFrame.right=InputFrame.left+21;
  34.     InputFrame.top=ToolR.top+50;
  35.     InputFrame.bottom=InputFrame.top+21;
  36.     
  37.     /*    Set up the editor pane to take up the rest of window space. */
  38.     EditR=PortRect;
  39.     EditR.left=(ToolR.right>MenuR.right) ? ToolR.right : MenuR.right;
  40.     InsetRect(&EditR,5,5);
  41.     EditOutline=EditR;
  42.     InsetRect(&EditOutline,-1,-1);
  43.     
  44.     /*    Create a region to contain the gray area outside all panes. */
  45.     OpenRgn();
  46.     FrameRect(&PortRect);
  47.     FrameRect(&ToolR);
  48.     FrameRect(&MenuR);
  49.     FrameRect(&EditR);
  50.     CloseRgn(BackRegion);
  51. }
  52. /*
  53. >>    When a mousedown is detected, one has to find what was clicked
  54. >>    and act accordingly. This is actually very simple stuff...nuff said.
  55. */
  56. void    DoMouseDown()
  57. {
  58.     int         Place;
  59.     Point        MousePoint;
  60.     WindowPtr    TheWind;
  61.     Rect        BigGrow={200,180,32000,32000};
  62.     Rect        DragRect={-32000,-32000,32000,32000};
  63.     long        NewWindowSize;
  64.     
  65.     Place=FindWindow(MyEvent.where,&TheWind);
  66.     switch(Place)
  67.     {    case inMenuBar:
  68.             break;
  69.         case inDrag:
  70.             DragWindow(TheWind,MyEvent.where,&DragRect);
  71.             break;
  72.         case inGrow:
  73.             NewWindowSize = GrowWindow(TheWind,MyEvent.where,&BigGrow);
  74.             SizeWindow(TheWind,LoWord(NewWindowSize),HiWord(NewWindowSize),0);
  75.             SetPort(TheWind);
  76.             InvalRect(&TheWind->portRect);
  77.             PortRect = TheWind->portRect;
  78.             CalculatePanelPlaces();
  79.             break;
  80.         case inZoomIn:
  81.         case inZoomOut:
  82.             if(TrackBox(TheWind,MyEvent.where,Place))
  83.             {    SetPort(TheWind);
  84.                 ZoomWindow(TheWind,Place,0);
  85.                 InvalRect(&TheWind->portRect);
  86.                 PortRect = TheWind->portRect;
  87.                 CalculatePanelPlaces();
  88.             }
  89.             break;
  90.         case inContent:
  91.             if(TheWind==FrontWindow())
  92.             {    MousePoint=MyEvent.where;
  93.                 SetPort(MyWind);
  94.                 GlobalToLocal(&MousePoint);
  95.                 if(PtInRect(MousePoint,&MenuR))
  96.                     DoMenuClick();
  97.                 else
  98.                 if(PtInRect(MousePoint,&ToolR))
  99.                     DoToolClick();
  100.                 else
  101.                 if(PtInRect(MousePoint,&EditR))
  102.                     DoEditClick();
  103.             }
  104.             else
  105.             {    SelectWindow(TheWind);
  106.             }
  107.             break;
  108.         case inGoAway:
  109.             if(TrackGoAway(TheWind,MyEvent.where))
  110.             {    HideWindow(TheWind);
  111.                 QuitNow= -1;
  112.             }
  113.             break;
  114.         case inSysWindow:
  115.             SystemClick(&MyEvent,TheWind);
  116.             break;
  117.     }
  118. }
  119.  
  120. /*
  121. >>    Redraw main window contents when an update event is received.
  122. >>    If this update is for some other window, we're in deep
  123. >>    sh*t, since there should be just one window.
  124. */
  125. void    DoUpdate()
  126. {
  127.     WindowPtr    TheWind;
  128.     
  129.     TheWind=(WindowPtr)MyEvent.message;
  130.  
  131.     BeginUpdate(MyWind);
  132.     SetPort(MyWind);
  133.  
  134.     if(TheWind==MyWind)
  135.     {    
  136.         DrawPicture(MenuP,&MenuR);
  137.         HILITEMODE;
  138.         InvertRect(&MenuHilite);
  139.         DrawPicture(ToolP,&ToolR);
  140.         UpdateButtonTags();
  141.         FillRgn(BackRegion,gray);
  142.         FrameRect(&EditOutline);
  143.         ClipEditArea();     
  144.         UpdateSim();
  145.         RestoreClipping();
  146.     }
  147.     else
  148.     {    EraseRect(&PortRect);
  149.     }
  150.     EndUpdate(TheWind);
  151. }
  152. /*
  153. >>    UISetup loads PICTs and bitmaps, sets up some
  154. >>    rectangle structures that divide our window into
  155. >>    subpanes. If you want to add resizing windows,
  156. >>    some of this code has to be moved to a resize
  157. >>    handler. See X-version for details on how to do it.
  158. */
  159. void    UISetup()
  160. {
  161.     int         i;
  162.     Handle        filenamer;
  163.     Rect        TitleRect,TitleFrame;
  164.     PicHandle    TitleHand;
  165.  
  166.     /*    Create our main window and find its size.        */
  167.     MyWind=GetNewWindow(128,0,(WindowPtr)-1);
  168.     SetPort(MyWind);
  169.     PortRect=MyWind->portRect;
  170.  
  171.     /*    Load pictures for tool and menu panes.            */
  172.     MenuP=(PicHandle)GetResource('PICT',128);
  173.     ToolP=(PicHandle)GetResource('PICT',129);
  174.     HLock(MenuP);
  175.     HLock(ToolP);
  176.     
  177.     BackRegion=NewRgn();
  178.     CalculatePanelPlaces();
  179.     
  180.     SetRect(&NilRect,0,0,0,0);    /*    Just a convenience. */
  181.  
  182.     /*    Load a bitmap with misc stuff. Equivalent of misc.xbm        */
  183.     PictBit(&ButtonBits,130);
  184.  
  185.     /*    Set up default tools and lock them to three mouse buttons.    */
  186.     for(i=0;i<MOUSE_BUTTONS;i++)
  187.     {    ToolButtons[i].Function=17+i;
  188.         SetRect(&ToolButtons[i].Prime,
  189.                     ToolR.left+33+i*24,ToolR.bottom-26,
  190.                     ToolR.left+56+i*24,ToolR.bottom-3);
  191.         ToolLocks[i]=ToolButtons[i];
  192.     }
  193.     
  194.     /*    Default file name is "Untitled". Read name from resource.    */
  195.     filenamer=(Handle)GetString(128);
  196.     BlockMove(*filenamer,NameOfOpenFile.fName,GetHandleSize(filenamer));
  197.     FileIsNamed=0;
  198.     
  199.     /*    Draw window contents.                                        */
  200.     MyEvent.message=(long)MyWind;
  201.     DoUpdate();
  202.     
  203.     /*    Draw a splash screen containing a copyright message.        */
  204.     SetPort(MyWind);
  205.     TitleHand=(PicHandle)GetResource('PICT',131);
  206.     HLock(TitleHand);
  207.     TitleFrame=(*TitleHand)->picFrame;
  208.     OffsetRect(&TitleFrame,-TitleFrame.left,-TitleFrame.top);
  209.     OffsetRect(&TitleFrame,-TitleFrame.right/2,-TitleFrame.bottom/2);
  210.     OffsetRect(&TitleFrame, EditR.left+(EditR.right-EditR.left)/2,
  211.                             EditR.top+(EditR.bottom-EditR.top)/2);
  212.     TitleRect=TitleFrame;
  213.     InsetRect(&TitleFrame,-32,-32);
  214.     EraseRect(&TitleFrame);
  215.     FrameRect(&TitleFrame);
  216.     DrawPicture(TitleHand,&TitleRect);
  217.     HUnlock(TitleHand);
  218.     ReleaseResource(TitleHand);
  219.     SetTrashRect(&TitleFrame);
  220.     SplashVisible=-1;
  221. }
  222.  
  223. void    main()
  224. {
  225.     char            thechar;
  226.     unsigned long    NextSimulation;
  227.  
  228.     NextSimulation=0;
  229.  
  230.     DoInits();        /*    Initialize toolbox managers         */
  231.     SimSetup();     /*    Start up simulator variables        */
  232.     UISetup();        /*    Set up windows and stuff like that    */
  233.     
  234.     QuitNow=0;        /*    Exit application, when QuitNow= -1    */
  235.     
  236.     while(!QuitNow)
  237.     {    SystemTask();
  238.         if(GetNextEvent(everyEvent,&MyEvent))
  239.         {    switch(MyEvent.what)
  240.             {    case mouseDown:
  241.                     DoMouseDown();
  242.                     break;
  243.                 case keyDown:
  244.                 case autoKey:
  245.                     thechar=MyEvent.message;
  246.                     if(thechar>='0' && thechar <= '9')
  247.                     {    SimSpeed=3*(thechar-'0');
  248.                     }
  249.                     else
  250.                     if(thechar=='f')
  251.                     {    ConnectorFrames = ! ConnectorFrames;
  252.                         InvalRect(&EditR);
  253.                     }
  254.                     break;
  255.                 case updateEvt:
  256.                     DoUpdate();
  257.                     break;
  258.                 case activateEvt:
  259.                     SetPort(&MyWind);
  260.                     break;
  261.             }
  262.         }
  263.         
  264.         if(MyEvent.when>=NextSimulation)
  265.         {    SimLevel=0;         /*    Top level simulation.        */
  266.             SimTimer++;         /*    Counter for clock chips.    */
  267.             ClipEditArea();
  268.             RunSimulation();
  269.             RestoreClipping();
  270.             NextSimulation=MyEvent.when+SimSpeed;
  271.         }
  272.     }
  273. }
  274.  
  275. #endif
  276.